package cn.lin.controller;

import cn.lin.domain.User;
import cn.lin.service.UserService;
import cn.lin.util.Result;
import cn.lin.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    //uni-app登录通过手机号和密码查询user,返回UserVO对象
    @RequestMapping("/findUser")
    public List<UserVO> findUser(String tel, String pwd) {
        return userService.findUserVO(tel,pwd);
    }

    @RequestMapping("/update")
    public int updateNickname(@RequestParam("id") Integer id, @RequestParam("username") String username) {
        int i = userService.updateNickname(id, username);
        System.out.println(id+username);
        return i;
    }

    @RequestMapping("/updatePhone")
    public int updatePhone(@RequestParam("id") Integer id, @RequestParam("tel") String tel) {
        int i = userService.updatePhone(id,tel);
        System.out.println(id+tel);
        return i;
    }

    @RequestMapping("/findUserById")
    public User findUserById(@RequestParam("id") Integer id) {
        return userService.findUserById(id);
    }

    //通过组织id，去查找学生信息
    @RequestMapping("/findStu")
    public Object findStuByOrgId(@RequestParam("organizationId") Integer organizationId){
        List<User> students = userService.findAllStudent(organizationId);
        if (students == null || students.isEmpty()){
            return Result.error("查询失败") ;
        }
        return Result.ok("查询成功",students) ;
    }

    @RequestMapping("/findClassStu")
    public Object findClassStu(@RequestParam("classId") Integer classId){
        List<User> students = userService.findClassStu(classId);
        if (students == null || students.isEmpty()){
            return Result.error("查询失败") ;
        }
        return Result.ok("查询成功",students) ;
    }

    @RequestMapping("/delClassStuByStuId")
    public Object delClassStuByStuId(@RequestParam("classId") Integer classId, @RequestParam("userId") Integer userId){
        int i = userService.delClassStuByStuId(classId,userId);
        if (i == 0){
            return Result.error("删除失败") ;
        }
        return Result.ok("删除成功") ;
    }

    @RequestMapping("/addClassStu")
    public Object addClassStu(@RequestParam("classId") Integer classId, @RequestParam("studentsId") List<Integer> studentsId){
        int i = userService.addClassStu(classId,studentsId) ;
        if (i == 0){
            return Result.error("添加失败") ;
        }
        return Result.ok("添加成功");
    }

    @RequestMapping("/findByNameLike")
    public Object findByNameLike(@RequestParam(name = "name",defaultValue = "") String name){
        return userService.findByNameLike(name) ;
    }

    @RequestMapping("/importAll")
    public boolean importAll(){
        return userService.importAll();
    }

}
